﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ECare.Client.templates;
using ECare.BUS;
using ECare.BUS.Data;

namespace ECare.Client.frms
{
    public partial class frmNewOutInfo : frmInfo
    {
        public frmNewOutInfo()
        {
            InitializeComponent();

            this.Load += frmNewOutInfo_Load;

            this.grdDetails.AllowUserToAddRows = true;
            this.grdDetails.AllowUserToDeleteRows = true; this.grdDetails.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdDetails.DataError += grdDetails_DataError;

        }

        void grdDetails_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void frmNewOutInfo_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //加载用户 
            this.cbUsers.DataSource = UserInfoHelper.GetUsers();
            this.cbUsers.DisplayMember = "UserName";
            this.cbUsers.ValueMember = "UserId";


            //加载物料 

            resource_id.DataSource = ResourcesHelper.GetAllResources();
            resource_id.DisplayMember = "ResourceName";
            resource_id.ValueMember = "ResourceId";


            if (this.entity == null)
            {
                this.txtOutcode.Text = DateTime.Now.ToString("yyyyMMddhhmmss");
            }
            else
            {
                var outinfo = this.entity as PlanOutInfo;
                if (outinfo != null)
                {
                    this.txtOutcode.Text = outinfo.plan_out_code;
                    this.cbUsers.SelectedValue = outinfo.plan_out_get_by;
                    this.txtComments.Text = outinfo.plan_remark;

                    var details = new BindingList<PlanOutNeed>();

                    foreach (var key in outinfo.Details)
                    {
                        var newdetail = new PlanOutNeed();
                        newdetail.resourceId = key.Key;
                        newdetail.resourceNeeded = key.Value;
                        details.Add(newdetail);
                    }
                    this.grdDetails.DataSource = details;
                }
            }



        }

        private bool Validate()
        {if (string.IsNullOrEmpty(this.txtOutcode.Text))
            {
                this.errorProvider1.SetError(this.txtOutcode, "empty");
                return false;
            }
            else
            {
                this.errorProvider1.SetError(this.txtOutcode, "");
                //    return true;
            }

            if (this.grdDetails.Rows.Count == 0)
            {
                this.errorProvider1.SetError(this.btnSave, "请选择物料");
                return false;
            }
            else
            {
                this.errorProvider1.SetError(this.btnSave, "");
                //     return true;

            }

            return true;


        }
        public event EntitySavedHandler Saved;

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (Validate() == false)
                return;


            PlanOutInfo newout = null;

            if (this.entity == null)
            {
                newout = new PlanOutInfo
                {
                    plan_out_code = this.txtOutcode.Text,
                    plan_isactive = true,
                    plan_isout = false,
                    plan_out_check_status = CheckStatus.Pending,
                    plan_out_create_by = ApplicationData.CurrentUserInfo.UserId,
                    plan_out_create_time = DateTime.Now,
                    plan_out_get_by = int.Parse(this.cbUsers.SelectedValue.ToString()),
                    plan_out_get_time = DateTime.Now,
                    plan_out_update_by = ApplicationData.CurrentUserInfo.UserId,
                    plan_out_update_time = DateTime.Now,
                    plan_out_date = DateTime.Now,
                    plan_produce_date = DateTime.Now,
                    plan_remark = this.txtComments.Text
                };

             
            }
            else
            {
         
                newout = this.entity as PlanOutInfo;
                //this.txtOutcode.Text = newout.plan_out_code;
                //this.cbUsers.SelectedValue = newout.plan_out_get_by;
                //this.txtComments.Text = newout.plan_out_check_remark;

                newout.plan_out_code = this.txtOutcode.Text;
                newout.plan_out_get_by = int.Parse(this.cbUsers.SelectedValue.ToString());
                newout.plan_out_check_remark = this.txtComments.Text;

                //bding the detiales.
            }

            newout.Details = new Dictionary<int, decimal>();

            for (int i = 0; i < this.grdDetails.Rows.Count; i++)
            {

                if (this.grdDetails.Rows[i].IsNewRow)
                    continue;

                if (this.grdDetails.Rows[i].Cells[0].Value == null)
                    continue;

                if (string.IsNullOrEmpty(this.grdDetails.Rows[i].Cells[0].Value.ToString()))
                    continue;

                if (string.IsNullOrEmpty(this.grdDetails.Rows[i].Cells[1].Value.ToString()))
                    continue;


                int _resourceId = int.Parse(this.grdDetails.Rows[i].Cells[0].Value.ToString());
                decimal _needed = decimal.Parse(this.grdDetails.Rows[i].Cells[1].Value.ToString());

                if (newout.Details.ContainsKey(_resourceId))
                    newout.Details[_resourceId] = newout.Details[_resourceId] + _needed;
                else
                {
                    newout.Details.Add(_resourceId, _needed);
                }

            }

            int result = PlanHelper.CreatePlanOut(newout);
            newout.plan_out_id = result;


            if (Saved != null)
            {
                this.Saved(newout);
            }
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ((this.grdDetails.DataSource) as BindingList<PlanOutNeed>).AddNew();
        }
    }
}
